package sb.simple.wrapper.common;

public class CommonResult {

    //是否成功
    private boolean success;
    //状态码
    private int code;
    //提示信息
    private String msg;
    //数据
    private Object data;

    public CommonResult() {
    }

    public CommonResult(boolean success, int code, String msg, Object data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static CommonResult defineSuccess(Object data) {
        CommonResult result = new CommonResult();
        result.setSuccess(true);
        result.setCode(0);
        result.setMsg("");
        result.setData(data);
        return result;
    }

    //自定义异常返回结果
    public static CommonResult defineFailure(CustomException exception) {
        CommonResult result = new CommonResult();
        result.setSuccess(false);
        result.setCode(exception.getErrorCode());
        result.setMsg(exception.getErrorMsg());
        result.setData(null);
        return result;
    }

    //其他异常返回结果
    public static CommonResult otherFailure(OtherErrorEnum errorEnum) {
        CommonResult result = new CommonResult();
        result.setSuccess(false);
        result.setCode(errorEnum.getErrorCode());
        result.setMsg(errorEnum.getErrorMsg());
        result.setData(null);
        return result;
    }


    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "CommonResult{" + "success=" + success + ", code=" + code + ", msg='" + msg + '\'' + ", data=" + data + '}';
    }
}
